﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Models;
using UpdatePro;

namespace Library
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.tssl_AdminName.Text = Program.CurrentAdmin.AdminName;
            this.lblAdminRoleName.Text = Program.CurrentAdmin.RoleId.ToString();
            //权限设定
            InitRighes();
        }
        #region 权限设定
        /// <summary>
        /// 权限初始化
        /// </summary>
        public void InitRighes()
        {
            //超级管理员不做任何权限设置,拥有所有权限
            if (Program.CurrentAdmin.RoleId == 10)return;
            //只有超级管理员才能对分配系统权限，因此如果不是超级管理员的直接禁用掉权限管理按钮
            this.timsRightManager.Enabled = false;
            //一般管理员只设置不能设置权限管理,拥有所有权限除了设置权限的一切权限
            if (Program.CurrentAdmin.RoleId == 11) return;

            //设置一级菜单
            List<string> firstMenu = Program.CurrentAdmin.FirstMenu;
            this.tsmi_BookManager.Enabled = firstMenu.Contains(this.tsmi_BookManager.Tag.ToString());
            this.tsmi_BorrowAndReturn.Enabled = firstMenu.Contains(this.tsmi_BorrowAndReturn.Tag.ToString());
            this.tsmi_ReaderManager.Enabled = firstMenu.Contains(this.tsmi_ReaderManager.Tag.ToString());
            #region 设置二级菜单权限：快捷键对应二级菜单
            //获取二级菜单权限列表
            List<string> secondMenu = Program.CurrentAdmin.SecondMenu;
            //修改密码
            this.tsmiModifyPwd.Enabled = secondMenu.Contains(this.tsmiModifyPwd.Tag.ToString());
            this.btnModifyPwd.Enabled = secondMenu.Contains(this.btnModifyPwd.Tag.ToString());
            //新增图书
            this.tsmiAddBook.Enabled = secondMenu.Contains(this.tsmiAddBook.Tag.ToString());
            this.btnAddBook.Enabled = secondMenu.Contains(this.btnAddBook.Tag.ToString());
            //图书上架
            this.tsmiBookNew.Enabled = secondMenu.Contains(this.tsmiBookNew.Tag.ToString());
            this.btnBookNew.Enabled = secondMenu.Contains(this.btnBookNew.Tag.ToString());
            //图书管理（图书信息维护）
            this.tsmiBookManage.Enabled = secondMenu.Contains(this.tsmiBookManage.Tag.ToString());
            this.btnBookManage.Enabled = secondMenu.Contains(this.btnBookManage.Tag.ToString());
            //图书出借
            this.tsmiBorrowBook.Enabled = secondMenu.Contains(this.tsmiBorrowBook.Tag.ToString());
            this.btnBorrowBook.Enabled = secondMenu.Contains(this.btnBorrowBook.Tag.ToString());

            //图书归还
            this.tsmiReturnBook.Enabled = secondMenu.Contains(this.tsmiReturnBook.Tag.ToString());
            this.btnReturnBook.Enabled = secondMenu.Contains(this.btnReturnBook.Tag.ToString());
            //会员管理
            this.tsmi_ReaderManager0.Enabled = secondMenu.Contains(this.tsmi_ReaderManager0.Tag.ToString());
            this.btnReaderManager.Enabled = secondMenu.Contains(this.btnReaderManager.Tag.ToString());
            #endregion
        }
        #endregion

        #region 子窗体嵌入
        private void OpenForm(Form from)
        {
            foreach (Control item in this.splitContainer1.Panel2.Controls)
            {
                if (item is Form)
                {
                    ((Form)item).Close();
                }
            }
            from.TopLevel = false;
            from.Parent = this.splitContainer1.Panel2;
            from.Dock = DockStyle.Fill;
            from.FormBorderStyle = FormBorderStyle.None;
            from.Show();
        }
        #endregion

        //新增图书
        private void BtnAddBook_Click(object sender, EventArgs e)
        {
            FrmAddBook frmAddBook = new FrmAddBook();
            OpenForm(frmAddBook);
            this.lblOperationName.Text = "新增图书";
        }

        //图书上架
        private void BtnBookNew_Click(object sender, EventArgs e)
        {
            FrmNewBook frmNewBook = new FrmNewBook();
            OpenForm(frmNewBook);
            this.lblOperationName.Text = "图书上架";
        }

        //图书归还
        private void BtnReturnBook_Click(object sender, EventArgs e)
        {
            FrmReturnBook frmReturnBook = new FrmReturnBook();
            OpenForm(frmReturnBook);
            this.lblOperationName.Text = "图书归还";
        }

        //图书管理
        private void BtnBookManage_Click(object sender, EventArgs e)
        {
            FrmBookManage frmBookManage = new FrmBookManage();
            OpenForm(frmBookManage);
            this.lblOperationName.Text = "图书管理";
        }

        //会员管理
        private void BtnReaderManager_Click(object sender, EventArgs e)
        {
            FrmReaderManger frmReaderManger = new FrmReaderManger();
            OpenForm(frmReaderManger);
            this.lblOperationName.Text = "会员管理";
        }

        //图书借阅
        private void BtnBorrowBook_Click(object sender, EventArgs e)
        {
            FrmBorrowBook frmBorrowBook = new FrmBorrowBook();
            OpenForm(frmBorrowBook);
            this.lblOperationName.Text = "图书借阅";
        }

        //密码修改
        private void BtnModifyPwd_Click(object sender, EventArgs e)
        {
            FrmModifyPwd frmModifyPwd = new FrmModifyPwd();
            frmModifyPwd.ShowDialog();
        }

        //退出系统
        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //退出系统时的询问
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("确认退出系统吗？", "退出询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Cancel)
            {
                e.Cancel = true;//取消窗体关闭操作
            }
        }

        //权限管理
        private void timsRightManager_Click(object sender, EventArgs e)
        {
            FrmRightManager frmRightManager = new FrmRightManager();
            frmRightManager.ShowDialog();
        }

        //系统升级
        private void 系统升级ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            UpdateManager updateManager = null;
            try
            {
                updateManager = new UpdateManager();
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法连接服务器，请检查网络设置："+ex.Message);
            }
            if (updateManager.IsUpdate)
            {
                if (MessageBox.Show("为了更新文件，将退出当前程序，请确认保存数据！确认退出吗？", "升级提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //关闭当前程序
                    Application.ExitThread();
                    Application.Exit();
                    //启动升级程序
                    Process.Start("UpdatePro.exe");
                }
            }
            else
            {
                MessageBox.Show("当前版本已经是最新版本，不需要升级！","提示信息");
                
            }
        }


        /// <summary>
        /// 定时检测是否需要升级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateManager updateManager = null;
            try
            {
                updateManager = new UpdateManager();
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法连接服务器，请检查网络设置：" + ex.Message);
            }

            if (updateManager.IsUpdate)
            {
                FrmTips frmTips = new FrmTips();
                frmTips.Show();
            }
            //检查后停止定时器，防止频繁访问服务器
            this.timer1.Enabled = false;
        }
    }
}
